<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>网站后台管理模版</title>
    <link rel="stylesheet" type="text/css" href="./static/admin/layui/css/layui.css"/>
    <link rel="stylesheet" type="text/css" href="./static/admin/css/admin.css"/>
</head>
<body>
<div class="layui-tab page-content-wrap">
    <ul class="layui-tab-title">
        <li class="layui-this">修改资料</li>
        <li>修改密码</li>
    </ul>
    <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
            <form class="layui-form" style="width: 90%;padding-top: 20px;">
                <div class="layui-form-item">
                    <label class="layui-form-label">ID：</label>
                    <div class="layui-input-block">
                        <input type="text" name="u_id" disabled autocomplete="off" class="layui-input layui-disabled"
                               value="">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名：</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" required lay-verify="required" placeholder="请输入标题"
                               autocomplete="off" class="layui-input" value="">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱：</label>
                    <div class="layui-input-block">
                        <input type="text" name="email" required lay-verify="required" placeholder="请输入标题"
                               autocomplete="off" class="layui-input" value="">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">手机号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="phone" required lay-verify="required" placeholder="请输入标题"
                               autocomplete="off" class="layui-input" value="">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="adminInfo">立即提交</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="layui-tab-item">
            <form class="layui-form" v style="width: 90%;padding-top: 20px;">
                <div class="layui-form-item">
                    <label class="layui-form-label">ID：</label>
                    <div class="layui-input-block">
                        <input type="text" name="u_id" disabled autocomplete="off" class="layui-input layui-disabled"
                               value="">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名：</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" required lay-verify="required"
                               class="layui-input" value="">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">旧密码：</label>
                    <div class="layui-input-block">
                        <input type="password" name="password1" required lay-verify="required" placeholder="请输入密码"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">新密码：</label>
                    <div class="layui-input-block">
                        <input type="password" name="password2" required lay-verify="required" placeholder="请输入密码"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">重复密码：</label>
                    <div class="layui-input-block">
                        <input type="password" name="password3" required lay-verify="required" placeholder="请输入密码"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="adminPassword">立即提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="./static/admin/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="./static/js/jquery-1.12.4.js"></script>

<script>
    //Demo
    layui.use(['form', 'element'], function () {
        var form = layui.form();
        var element = layui.element();
        form.render();
        //监听信息提交
        form.on('submit(adminInfo)', function (data) {
            // layer.msg(JSON.stringify(data.field));
            $.ajax(
                {
                    url: '/api/user',
                    type: 'POST',
                    data: JSON.stringify(data.field),
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (obj) {

                        if (obj.code == 20) {
                            alert("更新成功了");
                            layer.msg("更新成功", {time: 3});
                            time.sleep(3);
                            // $(location).attr('href', 'index.html');
                            location.reload();
                        } else {
                            alert(obj.msg);
                        }
                    },
                    error: function (obj) {
                        alert('未知错误');
                    }
                }
            );
            return false;
        });
        //监听密码提交
        form.on('submit(adminPassword)', function (data) {
            layer.msg(JSON.stringify(data.field));
            $.ajax(
                {
                    url: '/api/user',
                    type: 'POST',
                    data: JSON.stringify(data.field),
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (obj) {

                        if (obj.code == 20) {
                            alert("更新成功了，页面会重新加载");
                            // layer.msg("更新成功", {time: 3});
                            // time.sleep(3);
                            // $(location).attr('href', 'index.html');
                            location.reload();
                        } else {
                            alert(obj.msg);
                        }
                    },
                    error: function (obj) {
                        alert('未知错误');
                    }
                }
            );
            return false;
        });
    });
</script>

<script>
    user_info();

    function user_info() {
        //确认有没有登录
        $.ajax({
            "url": "/api/user",
            "type": "get",
            "dataType": 'json',
            xhrFields: {
                withCredentials: true
            },
            error: function () {
                console.log("登录获取失败")
            }, crossDomain: true,
            success: function (data) {
                if (data.code == 20) {
                    $("input[name='u_id']").val(data.data['user_id']);
                    $("input[name='name']").val(data.data['name']);
                    $("input[name='email']").val(data.data['email']);
                    $("input[name='phone']").val(data.data['phone']);
                } else {
                    alert("还没登录/登录超时了~");
                    window.location.href = '/login.html'
                }
                // if (data.code == 0) {
                //     $('#user').html(data.msg)
                // } else {
                //     alert(data.msg);
                //     window.location.href = 'login.html';
                // }
            }
        })
    }
</script>
</body>
</html>